package online.wenmeng.service;

import online.wenmeng.bean.*;
import online.wenmeng.config.Config;
import online.wenmeng.dao.FilmuserMapper;
import online.wenmeng.dao.SpellgroupsMapper;
import online.wenmeng.dao.SpellgroupsuserMapper;
import online.wenmeng.exception.ParameterErrorException;
import online.wenmeng.utils.MyUtils;
import online.wenmeng.utils.TransitionUtil;
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 狼芒
 * @Date: 2021/3/11 0:16
 * @Descrintion: 团约信息服务类
 * @version: 1.0
 */
@Service
public class SpellGroupsService {

    @Autowired
    private SpellgroupsMapper spellgroupsMapper;
    @Autowired
    private FilmuserMapper filmuserMapper;
    @Autowired
    private SpellgroupsuserMapper spellgroupsuserMapper;

    /**
     * 查看所有团约
     * @return
     */
    public Map<String, Object> getSpellGroup() {
        List<Spellgroups> spellgroups = spellgroupsMapper.selectByExample(new SpellgroupsExample());
        return MyUtils.getNewMap(Config.SUCCESS,null,null,spellgroups);
    }

    public Map<String, Object> addSpellGroup(Spellgroups spellGroup) throws ParameterErrorException {
        int spellGroupId;
        while (true){
            spellGroupId = MyUtils.getRandomNum();
            if (spellgroupsMapper.selectByPrimaryKey(spellGroupId)==null){
                break;
            }
        }
        spellGroup.setSpellgroupid(spellGroupId);
        int insert = spellgroupsMapper.insert(spellGroup);
        if (insert>0){
            return MyUtils.getNewMap(Config.SUCCESS,null,null,getSpellGroup());
        }
        throw new ParameterErrorException();
    }

    public Map<String, Object> deleteSpellGroup(int spellGroupId) throws ParameterErrorException {

        int i = spellgroupsMapper.deleteByPrimaryKey(spellGroupId);
        if (i>0){
            return  MyUtils.getNewMap(Config.SUCCESS,null,null,getSpellGroup());
        }
        throw new ParameterErrorException();
    }

    public Map<String, Object> getSpellGroupInfo(int spellGroupId) throws ParameterErrorException {
        Spellgroups spellgroups = spellgroupsMapper.selectByPrimaryKey(spellGroupId);
        if (spellgroups!=null){
            //获取到所有用户信息
            String[] userids = spellgroups.getUserid().split(Config.splitUsers);
            List<Filmuser> filmuserList = new ArrayList<>();
            for (String userid:userids) {
                if (userid==null||userid.trim().length()==0){
                    continue;
                }
                Filmuser filmuser = filmuserMapper.selectByPrimaryKey(TransitionUtil.transitionType(userid, int.class));
                if (filmuser!=null){
                    filmuserList.add(filmuser);
                }
            }
            return  MyUtils.getNewMap(Config.SUCCESS,null,null,filmuserList);
        }
        throw new ParameterErrorException();
    }

    public Map<String, Object> addSpellGroupInfo(HttpSession session, int spellGroupId) throws ParameterErrorException {
        Map<String, Object> userInfo = (Map<String, Object>) session.getAttribute(Config.userInfoInRun);
        Integer userId = TransitionUtil.transitionType(userInfo.get(Config.Openid), int.class);
        Spellgroups spellgroups = spellgroupsMapper.selectByPrimaryKey(spellGroupId);
        if (spellgroups!=null){

            String userIds = "";
            String[] userids = spellgroups.getUserid().split(Config.splitUsers);
            for (int i = 0; i < userids.length-1; i++) {
                userIds+=userids[i]+Config.splitUsers;
            }

            Filmuser filmuser = filmuserMapper.selectByPrimaryKey(userId);
            //生成我的团约信息
            Spellgroupsuser spellgroupsuser = new Spellgroupsuser();
            spellgroupsuser.setSpellgroupid(spellGroupId);
            spellgroupsuser.setUserid(userId);
            spellgroupsuser.setGender((String) userInfo.get(Config.Gender));
            spellgroupsuser.setUsername((String) userInfo.get(Config.NickName));
            spellgroupsuser.setUseravatar(Config.ChatHead);
            spellgroupsuser.setQq(TransitionUtil.transitionType(filmuser.getQq(),long.class));
            spellgroupsuser.setPhone(TransitionUtil.transitionType(filmuser.getPhone(),long.class));
            spellgroupsuser.setJointime(new Date());
            spellgroupsuser.setIsingroup(Config.filmPlayerinfo_ing);


            //如果拼车完成
            if (spellgroups.getFilmcount()<=spellgroups.getRealcount()){
                throw new ParameterErrorException();
            }else if (spellgroups.getFilmcount()<=spellgroups.getRealcount()+1){
                spellgroupsuser.setIsingroup(Config.filmPlayerinfo_end);
                //更改所有人拼车完成
                SpellgroupsuserExample spellgroupsuserExample = new SpellgroupsuserExample();
                spellgroupsuserExample.createCriteria().andSpellgroupidEqualTo(spellGroupId);
                List<Spellgroupsuser> spellgroupsusers = spellgroupsuserMapper.selectByExample(spellgroupsuserExample);
                for (Spellgroupsuser spellgroupsuser1:spellgroupsusers){
                    if (spellgroupsuser1==null){
                        continue;
                    }
                    spellgroupsuser1.setIsingroup(Config.filmPlayerinfo_end);
                    spellgroupsuserMapper.updateByPrimaryKey(spellgroupsuser1);
                }
            }


            int insertNum = spellgroupsuserMapper.insert(spellgroupsuser);
            if (insertNum>0){
                userIds+=userids[userids.length-1];
                spellgroups.setUserid(userIds);
                spellgroups.setRealcount(spellgroups.getRealcount()+1);
                spellgroupsMapper.updateByPrimaryKey(spellgroups);

                return  MyUtils.getNewMap(Config.SUCCESS,spellgroupsuser,filmuser,spellgroups);
            }
            return MyUtils.getNewMap(Config.ERROR,filmuser,"Already in the regiment",spellgroups);
        }
        throw new ParameterErrorException();
    }

    /**
     * 查询我所有的团约信息
     * @param session
     * @return
     */
    public Map<String, Object> findMySpellGroup(HttpSession session) {
        List<Spellgroups> mySpell = new ArrayList<>();
        Integer userId = MyUtils.getOpenIdBySession(session);
        SpellgroupsExample spellgroupsExample = new SpellgroupsExample();
        List<Spellgroups> spellgroups = spellgroupsMapper.selectByExample(spellgroupsExample);
        for (Spellgroups spellgroups1:spellgroups){
            List<Integer> userIdList = MyUtils.getAllUser2Spellgroups(spellgroups1);
            if (userIdList.contains(userId)){
                mySpell.add(spellgroups1);
            }
        }
        return MyUtils.getNewMap(Config.SUCCESS,null,null,mySpell);
    }
}
